# -*- coding: utf-8 -*-
# File : datetime_utils.py
# Author: taoyahui
# Date : 2021/10/18
from datetime import date, datetime, timedelta
import json
import time


class ComplexEncoder(json.JSONEncoder):
    def default(self, obj):
        if isinstance(obj, datetime):
            return obj.strftime('%Y-%m-%d %H:%M:%S')
        elif isinstance(obj, date):
            return obj.strftime('%Y-%m-%d')
        else:
            return json.JSONEncoder.default(self, obj)


def time_delta_gen(start, end, delta):
    curr = start
    while curr < end:
        yield curr
        curr += delta

def str_to_timestamp(str):
    """将字符串%Y-%m-%dT%H:%M:%S.%fZ格式，转变为时间戳

    Args:
        str (_type_): _description_

    Returns:
        _type_: _description_
    """
    t = time.strptime(str,"%Y-%m-%dT%H:%M:%S.%fZ")
    ts =  int(time.mktime(t))
    return ts # 为以秒为单位的时间戳

def timestamp_to_str(timestamp):
    """将timestamp转换为字符串

    Args:
        timestamp (_type_): _description_

    Returns:
        _type_: _description_
    """
    dt_object = datetime.fromtimestamp(timestamp)
    return dt_object.strftime("%Y-%m-%d %H:%M:%S")
 
